/*
 *  dna_httpc.h -- provide dna-system HTTP Client simple implementation.
 *
 *  ORIGINAL AUTHOR: Xu Chun (chun.xu@broadlink.com.cn)
 *
 *  Copyright (c) 2016 Broadlink Corporation
 */

#ifndef __DNA_HTTPC_H
#define __DNA_HTTPC_H

#ifdef __cplusplus
    extern "C" {
#endif

/**
 *  @brief HTTP session handle definition
 */
typedef void * dna_http_session_t;

/**
 *  @brief HTTP session hostname max length
 */
#define DNA_HOSTNM_MAX HOSTNM_MAX
/** @defgroup dna_http_enum Enum
  * @{
  */

/** @brief HTTP request methods */
enum {
    DNA_HTTP_GET = 0,                   /*!< retrieve information */
    DNA_HTTP_POST,                      /*!< request to accept new sub-ordinate of resource */
    DNA_HTTP_PUT,                       /*!< modify or create new resource referred to by URI */
};

/**
  * @}
  */


/**
 * Start an HTTP session.
 *
 * @par
 * This API starts a new HTTP session. It will create a socket and
 * then connect to the server given as a parameter.
 *
 * @param[in] hostname Host name or IP address.  The hostname should be in
 * the format [http://]hostname[:portno][/path/to/resource]. The fields
 * between the square brackets above are optional. The part specifying
 * the path to resource will be ignored as it is of no use during session
 * setup.
 *
 * @return Handle to the HTTP session, NULL on error.
 */
dna_http_session_t dna_http_session_open(const char * url);

/**
 * Set HTTP timeout .
 *
 * @par
 * This API set the http timeout 
 *
 * @param[in] handle : Handle returned from the call to dna_http_session_open()
 @param[in] timeout :  millisecond

 *
 * @return error or success 
 */
int dna_http_set_timeout(dna_http_session_t handle,unsigned int timeout);
 /**
  *Perform an HTTP request. 
 *
 * This API sends the prepared request header and content if
 * applicable.
 *
 * @note If this API fails and the caller decides to abandon this
 * session altogether it needs to call the API  dna_http_session_close()
 * explicitly. Failure to do this will cause undefined behaviour.
 *
 * @param[in] handle Handle returned from the call to dna_http_session_open()
 * @param[in] type request type (GET/POST/PUT)
 * @param[in] header http header (End Flag: name and value all set to NULL)
 * @param[in] data content data
 * @param[in] len content length
 *
 * @return DNA_SUCCESS on success
 * @return -DNA_E_IO if failed to send data to network
 * @return -DNA_E_INVAL for an invalid arguments.
 */
int dna_http_send_request(
    dna_http_session_t handle,
    int type,
    char * header[][2],
    const void * data,
    unsigned int len);

int dna_http_post_file(
		dna_http_session_t handle, 
		char * header[][2], 
		const char *filepath);

/**
 * Get a particular HTTP response header value.
 *
 * This API parses the response header received from the server and
 * returns the value corresponding to a given name. 
 *
 * @note Calling this API is \b optional. If the caller does not want to
 * read/analyse the header and knows beforehand what to expect, it can
 * directly call dna_http_read_content(). The API dna_http_read_content()
 * will internally read the HTTP response header. However, the API
 * dna_http_read_content() will return an error if the HTTP status code
 * returned is not 200. If you need to read content in cases where the
 * return status code is other than 200 you need to call
 * dna_http_get_response_hdr() or the current API first.
 *
 * @note If this API fails and the caller decides to abandon this
 * session altogether it needs to call dna_http_session_close()
 * explicitly. Failure to do this will cause undefined behaviour.
 *
 * @pre \a dna_http_send_request is called before this call. Calling
 * dna_http_get_response_hdr() is \b not mandatory but recommended.
 *
 * @param[in] handle Handle returned from the call to \a dna_http_session_open
 * @param[in] name The name of the field whose value you wish to
 * know
 * @param[in,out] value A pointer which will point to the value
 * corresponding to the name passed as second parameter.
 *
 * @return DNA_SUCCESS on success
 * @return -DNA_FAIL on error
 */
int dna_http_get_response_hdr(
    dna_http_session_t handle,
    const char * name,
    char ** value);

/**
 * Read data content from the stream.
 *
 * This API will read the data sent by the server and pass it back to the
 * caller.
 * 
 * @note Once you start reading the content, it is mandatory to read
 * the entire content before you begin the next HTTP transaction.
 *
 * @note If this API fails and the caller decides to abandon this
 * session altogether it needs to call dna_http_session_close()
 * explicitly. Failure to do this will cause undefined behaviour.
 * 
 * @param[in] handle Handle returned from the call to dna_http_session_open()
 * @param[in, out] buff Caller allocated buffer of size max_len
 * @param[in] size Size of the buffer. The data read will be less than
 * or equal
 * to this size, depending on the size of the resource.
 *
 * @return On success, the number of bytes read is returned. Return value
 * of zero is \a not an error case  and should be treated as a signal that
 * server is done with sending the data.
 * @return -DNA_FAIL if this API is called again after the previous call
 * retuned 0 or for any other error.
 */
int dna_http_read_content(
    dna_http_session_t handle,
    void * buff,
    unsigned int size);

/**
 * Close the session.
 *
 * This API will close the session represented by the given
 * handle. The socket associated with this session will be closed and thus
 * the TCP connection with the server will be terminated. No requests on
 * this same session will succeed after this API is called.
 *
 * @param[in] handle Handle returned from the previous call to
 * dna_http_session_open(). The handle will be set to NULL by the callee thus
 * effectively blocking further requests on the same session handle.
 *
 * @return Currently always returns DNA_SUCCESS
 */
int dna_http_session_close(dna_http_session_t handle);

/*We use the function in this case:We open a tcp connect and request a url(source),\
	now i wanna request a same hostname(ip:port) but a different resource*/

int dna_http_resource_relocate( dna_http_session_t handle,const char* url);
#ifdef __cplusplus
}
#endif

#endif

